package com.cn.wx.model;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 实体类
 */
public class TopModel {

    private String phone;
    private String img;
    private String ad;
    private String[] nav = {};
    private String[] href = {};

    /**
     * 通过在构造函数里面读取
     */
    public TopModel() {
        String phone = "028-87777888";
        String img = "../img/logo_g0yv.jpg";
        String ad = "<div class=\"div1\">品味生活·品味美食</div>\n" +
                "            <div class=\"div2\">\n" +
                "                <p>一壶茶沏出了生活的醇美</p>\n" +
                "                <p> 5一枝花开出了生活的鲜艳</p>\n" +
                "                <p> 3一首诗吟出了生活的酸辛</p>\n" +
                "                <p> 2一把火燃烧出了生活的激情</p>\n" +
                "                <p> 1一枝笔绘出了人生的五彩缤纷</p>\n" +
                "                <p> 一首歌唱出了人生的喜怒哀惧</p>\n" +
                "                <p> 生活像五味瓶，这么酸、这么甜、这么苦、又这么辣</p>\n" +
                "            </div>\n" +
                "            <div class=\"div3\">\n" +
                "                <span>品味</span>\n" +
                "                <span>生活</span>\n" +
                "                <span>品味</span>\n" +
                "                <span>美食</span>\n" +
                "            </div>\n" +
                "            <div class=\"div4\">\n" +
                "                新语餐饮给你生活指南\n" +
                "            </div>";
        String[] nav = {"首页", "公司", "新闻动态", "产品展示", "在线留言", "联系我们"};
        String[] href = {"/home", "/company", "/news", "/product", "/online", "/callus"};

        String pathname = "d:/top.txt";
        File file = null;//报错，是因为我们File里面没有空的构造函数

        FileOutputStream fileOutputStream = null;//大小管道的区别
        OutputStreamWriter outputStreamWriter = null;
        try {
            pathname = new String(pathname.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            file = new File(pathname);
            fileOutputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream);
            outputStreamWriter.write(phone);
            outputStreamWriter.write("#");
            outputStreamWriter.write(img);
            outputStreamWriter.write("#");
            outputStreamWriter.write(ad);
            outputStreamWriter.write("#");
            outputStreamWriter.write(Arrays.toString(nav));
            outputStreamWriter.write("#");
            outputStreamWriter.write(Arrays.toString(href));
            outputStreamWriter.flush();
            outputStreamWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<NavModel> getNavList() {

        String pathname = "d:/top.txt";
        File file = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            pathname = new String(pathname.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            file = new File(pathname);
            inputStreamReader = new InputStreamReader(new FileInputStream(file));
            bufferedReader = new BufferedReader(inputStreamReader);
            String a;
            StringBuilder stringBuilder = new StringBuilder();
            while ((a = bufferedReader.readLine()) != null) {
                stringBuilder.append(a);
            }
            String te = stringBuilder.toString();//将stringBuilder转换成string
            System.out.println(te);
//            te = te.substring(0, te.length() - 1);//不要最后一个@
            String[] aa = te.split("#");
            System.out.println("----------"+aa.length);
            this.phone = aa[0];
            this.img = aa[1];
            this.ad = aa[2];
            String navStr = aa[3];//{"首页", "公司", "新闻动态", "产品展示", "在线留言", "联系我们"};
//            navStr.replace(",",":");//第一个参数是老的字符，要被替换的字符，第二个参数是新的字符，要展示的字符
            navStr = navStr.substring(1, navStr.length() - 1);
            nav = navStr.split(",");
//navStr.contentEquals("首页")

            String hrefStr = aa[4];
            hrefStr = hrefStr.substring(1, hrefStr.length() - 1);
            href = hrefStr.split(",");
            System.out.println(this.phone);
            System.out.println(this.img);
            System.out.println(this.ad);
            System.out.println(navStr);
            System.out.println(hrefStr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        List<NavModel> list = new ArrayList<>();
        for (int i = 0; i < nav.length; i++) {
            NavModel navModel = new NavModel();
            navModel.setNav(nav[i]);
            navModel.setHref(href[i]);
            list.add(navModel);
        }
        return list;
    }

    /**
     * 封装，不需要别人修改，所以这里只设置了get，
     *
     * @return
     */
    public String getPhone() {
        return phone;
    }

    public String getImg() {
        return img;
    }

    public String getAd() {
        return ad;
    }

}
